﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
namespace GPBenhLanh
{
    public partial class frmHoanTien : DevExpress.XtraEditors.XtraForm
    {
        public frmHoanTien()
        {
            InitializeComponent();
        }

        TTXacNhanDTO Xn;
        public frmHoanTien(TTXacNhanDTO xnf)
        {
            InitializeComponent();
            Xn = xnf;
        }

        private void btnNhapHT_Click(object sender, EventArgs e)
        {
            //lay danh sach nhuom dac biet
            DataTable DataSource = (DataTable)gridMain.DataSource;
            DataTable dtChanger = DataSource.GetChanges(DataRowState.Modified);
            string UserName = "";
            string Host = Uit.it_Computer.ComputerName().ToString();
            int idBS = int.Parse(comBacSi.EditValue.ToString());
            string TenBS = comBacSi.Text;
            string MaDVu = "";
            int Tien = 0;
            int soluong = 0;
            if (dtChanger != null)
            {                
                for (int i = 0; i < dtChanger.Rows.Count; i++)
                {
                    if (dtChanger.Rows[i]["Checked"].ToString() == "1")
                    {
                        MaDVu = dtChanger.Rows[i]["MaDVu"].ToString();
                        Tien = int.Parse(dtChanger.Rows[i]["SoTien"].ToString());
                        soluong = int.Parse(dtChanger.Rows[i]["SoLuong"].ToString());
                        KQBUS.InsertCDHoanTien(txtSoTieuBan.Text, MaDVu, txtMauBenhPham.Text
                            , txtChanDoanGPB.Text, Tien, idBS, UserName, Host, soluong);
                    }
                }
            }
        }

        private void btnInPhieu_Click(object sender, EventArgs e)
        {
            frmPhieuHoanTien rp = new frmPhieuHoanTien();
            DataTable dtCDHT= GetThongTinSearchBUS.GetChiDinhHoanTien(txtSoTieuBan.Text);
          
            dsXacNhan ds = new dsXacNhan();
            DateTime CDate = new DateTime();
            DataRow dr;
            int TongTien = 0;
            int Solg = 0;
            string ma = "";
            if (dtCDHT != null)
            {
                for (int i = 0; i < dtCDHT.Rows.Count; i++)
                {
                    //dr = ds.dsHoanTien.NewRow();
                    //dr["MaDvu"] = 
                    //dr["TenDvu"] = dtCDHT.Rows[i]["TenDvu"].ToString();
                    //dr["Tien"] = int.Parse(dtCDHT.Rows[i]["Tien"].ToString());
                    //ds.dsHoanTien.Rows.Add(dr);
                    ma = dtCDHT.Rows[i]["MaDVu"].ToString();
                    TongTien += int.Parse(dtCDHT.Rows[i]["Tien"].ToString());

                    switch (ma)
                    {
                        case "TP21":
                            rp.TP21check.Checked = true;
                            rp.TP21check.Text  = " ";
                            rp.TP21SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP22":
                            rp.TP22check.Checked = true;
                            rp.TP22check.Text = " ";
                            rp.TP22SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP23":
                            rp.TP23check.Checked = true;
                            rp.TP23check.Text = " ";
                            rp.TP23SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP24":
                            rp.TP24check.Checked = true;
                            rp.TP24check.Text = " ";
                            rp.TP24SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP25":
                            rp.TP25check.Checked = true;
                            rp.TP25check.Text = " ";
                            rp.TP25SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP26":
                            rp.TP26check.Checked = true;
                            rp.TP26check.Text = " ";
                            rp.TP26SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP27":
                            rp.TP27check.Checked = true;
                            rp.TP27check.Text = " ";
                            rp.TP27SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        case "TP28":
                            rp.TP28check.Checked = true;
                            rp.TP28check.Text = " ";
                            rp.TP28SL.Text = dtCDHT.Rows[i]["SoLuong"].ToString();
                            break;
                        default:
                            break;
                    }
                    CDate = (DateTime)dtCDHT.Rows[i]["CDate"];
                    Solg += int.Parse(dtCDHT.Rows[i]["SoLuong"].ToString());
                }
                rp.txtTongTien.Text = TongTien.ToString("##,#0");
                rp.txtNgayCD.Text = "Ngày " + CDate.Day.ToString()
                                + " Tháng " + CDate.Month.ToString()
                                + " Năm " + CDate.Year.ToString();
            }
            rp.txtSoLuong.Text = Solg.ToString();
            rp.txtBSCD.Text = comBacSi.Text;
            rp.txttuoi.Text = txtTuoi.Text;
            rp.txtGioiTinh.Text = txtGioi.Text;
            rp.txtDiaChi.Text = txtdiachi.Text;
            rp.txtsonv.Text = txtSoNVien.Text;
            rp.txtChanDoanGPB.Text = txtChuanDoan.Text;
            rp.txtptv.Text = comptv.Text;
            rp.txtChanDoanGPB.Text = txtChanDoanGPB.Text;
            rp.txtMauBP.Text = txtMauBenhPham.Text;
            rp.txtSoTieuBan.Text = txtSoTieuBan.Text.ToUpper();
            rp.barSoHS.Text = txtsohs.Text;
            rp.txtSoBN.Text = txtsobn.Text;
            rp.txtNgayGioIn.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
            rp.txtPCIn.Text = Uit.it_Computer.ComputerName();
            rp.DataSource = ds.dsHoanTien;
            rp.ShowPreview();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            bool rs = KQBUS.DeleteGPB_CD_HoanTien(txtSoTieuBan.Text);
            if (rs)
            {
                MessageBox.Show("Xóa thành công!", "Thông báo");
                btngo_Click(sender, e);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btngo_Click(object sender, EventArgs e)
        {
            string SoTieuBan = txtSoTieuBan.Text;
            if (SoTieuBan.Equals(""))
                return;
            LoadGrid();
            DataTable dt = GetThongTinSearchBUS.SearchDaXacNhanBySoTB(SoTieuBan);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    btnNhapHT.Enabled = true;
                    txtHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                    txtTuoi.Text = dt.Rows[0]["namsinh"].ToString()
                                 + "(" + dt.Rows[0]["tuoi"].ToString() + " Tuổi)";
                    txtGioi.Text = dt.Rows[0]["gioi"].ToString();
                    txtdiachi.Text = dt.Rows[0]["DiaChi"].ToString();
                    txtsohs.Text = dt.Rows[0]["SoHS"].ToString();
                    txtsobn.Text = dt.Rows[0]["SoBN"].ToString();
                    txtSoNVien.Text = dt.Rows[0]["sonhapvien"].ToString();
                    txtChuanDoan.Text = dt.Rows[0]["chandoan"].ToString();
                    comptv.EditValue = int.Parse(dt.Rows[0]["ptvid"].ToString());
                    //da co chi dinh
                    DataTable dtCDHT = GetThongTinSearchBUS.GetChiDinhHoanTien(txtSoTieuBan.Text);
                    DataTable dtHT = (DataTable)gridMain.DataSource;
                    if (dtCDHT.Rows.Count > 0)
                    {
                        btnNhapHT.Enabled = false;
                        btnXoa.Enabled = true;

                        for (int i = 0; i < dtCDHT.Rows.Count; i++)
                        {
                            for (int j = 0; j < dtHT.Rows.Count; j++)
                            {
                                if (dtCDHT.Rows[i]["MaDVu"].ToString() == dtHT.Rows[j]["MaDVu"].ToString())
                                {
                                    dtHT.Rows[j]["Checked"] = 1;
                                }
                            }
                            txtMauBenhPham.Text = dtCDHT.Rows[i]["MauBenhPham"].ToString();
                            txtChanDoanGPB.Text = dtCDHT.Rows[i]["ChanDoanGPB"].ToString();
                            comBacSi.EditValue = int.Parse(dtCDHT.Rows[i]["IDBSiHoan"].ToString());
                        }
                    }
                    else
                    {
                        txtMauBenhPham.Text = "";
                        txtChanDoanGPB.Text = "";
                    }
                }
                else
                {
                    txtHoTen.Text = "";
                    txtTuoi.Text = "";
                    txtGioi.Text = "";
                    txtdiachi.Text = "";
                    txtsohs.Text = "";
                    txtsobn.Text = "";
                    txtMauBenhPham.Text = "";
                    txtChanDoanGPB.Text = "";

                }
            }
        }

        private void LoadGrid()
        {
            DataTable dtHoanTien = GetThongTinSearchBUS.GetDMHoanTien();
            gridMain.DataSource = dtHoanTien;
           
            DataTable dtBSChiDinh = GetThongTinSearchBUS.SearchDSBacSi();
            comBacSi.Properties.DataSource = dtBSChiDinh;
        }

        private void frmHoanTien_Load(object sender, EventArgs e)
        {
            LoadGrid();
            if (Xn != null)
            {
                txtChanDoanGPB.Text = "";
                txtChuanDoan.Text = Xn.ChuanDoan;
                txtdiachi.Text = Xn.Diachi;
                if (Xn.Gioi)
                    txtGioi.Text = "Nam";
                else
                    txtGioi.Text = "Nữ";
                txtHoTen.Text = Xn.HoTen;
                txtTuoi.Text = Xn.NamSinh + " (" + Xn.Tuoi.ToString() + ") ";
                txtsohs.Text = Xn.SoHS;
                txtsobn.Text = Xn.SoBN;
                txtSoNVien.Text = Xn.SoNV;
                txtSoTieuBan.Text = Xn.SoTieuBan;
                comptv.EditValue = Xn.PTVien;
            }
            btngo_Click( sender,  e);
        }

        private void groupControl2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}